'use strict';

const path = require('path');
const dataConst = require('../constant/data-const');
const fileSrv = require('../utility/fileSrv');

const Controller = require('egg').Controller;

class MeetingController extends Controller {
    async createMeeting() {
        const { app, ctx } = this;
        const { body } = ctx.request;
        const { title, type, desc } = body;

        if (!title) {
            return ctx.errorResult('title missing');
        }
        if (!type) {
            return ctx.errorResult('type missing');
        }
        if (!desc) {
            return ctx.errorResult('desc missing');
        }

        try {
            const [result, created] = await app.model.Meeting.findCreateFind({
                where: {
                    title
                },
                defaults: body
            });

            if (created) {
                ctx.successResult(result);
            } else {
                ctx.errorResult('meeting title duplicate');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async editMeeting() {
        const { app, ctx } = this;
        const { meetingId } = ctx.params;
        const { body } = ctx.request;
        const { title, type, desc, image, detail } = body;

        if (!title) {
            return ctx.errorResult('title missing');
        }
        if (!type) {
            return ctx.errorResult('type missing');
        }
        if (!desc) {
            return ctx.errorResult('desc missing');
        }
        if (!detail) {
            return ctx.errorResult('detail missing');
        }

        try {
            const resourceDir = path.join(app.resDir, `meeting/${meetingId}/detail`);
            const imageList = fileSrv.readDir(resourceDir);

            for (const image of imageList) {
                const result = detail.indexOf(image);
                if (result < 0) {
                    fileSrv.deleteFile(`${resourceDir}/${image}`);
                    console.log('delete image:', image);
                }
            }

            const result = await app.model.Meeting.findOne({
                where: {
                    id: meetingId
                }
            });
            if (result) {
                result.title = title;
                result.type = type;
                result.desc = desc;
                result.image = image;
                result.detail = detail;
                result.release = dataConst.stateType.active;
                await result.save();
                ctx.successResult(result);
            } else {
                ctx.errorResult('no meeting to update');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMeetingList() {
        const { ctx, service } = this;
        const { limit, offset, title } = ctx.query;

        try {
            const result = await service.meeting.getMeetingListSrv(title, limit, offset);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMeetingById() {
        const { ctx, service } = this;
        const { meetingId } = ctx.params;

        try {
            const result = await service.meeting.getMeetingDetailSrv(meetingId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async closeMeeting() {
        const { app, ctx } = this;
        const { meetingId } = ctx.params;

        try {
            const result = await app.model.Meeting.findById(meetingId);
            if (result) {
                result.state = dataConst.stateType.inactive;
                await result.save();
                ctx.successResult(result);
            } else {
                ctx.errorResult('no meeting to close');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }
}

module.exports = MeetingController;
